# -*- coding: utf-8 -*-
"""
@Time ： 2021/8/13 21:27
@Auth ： Mr.掌心 2929184523
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：接口数据驱动运行
"""
import time
import traceback

from common.Logger import logger, path
from common.excel.Excel import get_reader, get_writer
from common.mail import Mail
from keywords.interkeys import Inter


def runCase(line: list, obj, row):
    if len(line[0]) > 0 or len(line[1]) > 0:
        logger.debug('分组信息不执行')
    else:
        # 反射
        try:
            func = getattr(obj, line[3])
        except Exception as e:
            logger.error('关键字：%s不存在' % line[3])
            logger.exception(e)
            writer.write(row, 7, 'FAIL', 2)
            writer.write(row, 8, '关键字：%s不存在' % line[3])
            return

        # 处理参数
        line = line[4:]
        line = line[:line.index('')]
        try:
            line = line[:line.index(' ')]
        except:
            pass

        # 运行
        try:
            if line:
                res = func(*line)
            else:
                res = func()

            if type(res) == tuple:
                if res[0] == 1:
                    writer.write(row, 7, 'PASS', 3)
                else:
                    writer.write(row, 7, 'FAIL', 2)
            else:
                writer.write(row, 7, 'PASS', 3)
            writer.write(row, 8, str(res))
        except Exception as e:
            logger.error('用例执行错误')
            logger.exception(e)
            writer.write(row, 7, 'FAIL', 2)
            writer.write(row, 8, str(traceback.format_exc()))


casename = 'HTTP接口用例.xlsx'

reader = get_reader(path + '/lib/cases/' + casename)
writer = get_writer(path + '/lib/cases/' + casename, path + '/lib/cases/result-' + casename)

sheetname = reader.get_sheets()
lines = reader.readline()
casetype = lines[1][1]
if casetype == 'Inter':
    obj = Inter()
else:
    obj = Inter()

writer.set_sheet(sheetname[0])
start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
writer.write(1, 3, start_time)

logger.debug(sheetname)
for sheet in sheetname:
    # 设置当前读取的sheet页面
    reader.set_sheet(sheet)
    writer.set_sheet(sheet)

    lines = reader.readline()
    for i in range(1, len(lines)):
        logger.debug(lines[i])
        runCase(lines[i], obj, i)

writer.set_sheet(sheetname[0])
end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
writer.write(1, 4, end_time)
writer.save_close()

#发送邮件
mail = Mail()
mail.mail_info['filepaths'] = [path + '/lib/cases/result-' + casename]
mail.mail_info['filenames'] = ['result-' + casename]

text = mail.get_summary(path + '/lib/cases/result-' + casename)
trs = mail.get_details(path + '/lib/cases/result-' + casename)
text = text.replace('mailbody', trs)

# mail = Mail()
# mail.mail_info['filepaths'] = ['./lib/cases/result-HTTP接口用例.xlsx']
# mail.mail_info['filenames'] = ['result-HTTP接口用例.xlsx']
#
# text = mail.get_summary('./lib/cases/result-HTTP接口用例.xlsx')
# trs = mail.get_details('./lib/cases/result-HTTP接口用例.xlsx')
# text = text.replace('mailbody', trs)

mail.send(text)
